package com.share.quartz.service.imp;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;

import com.share.quartz.service.QuartzJobBean;
import com.share.quartz.service.IQuartzJobEntity;
import com.share.quartz.service.QuartzJobService;


public class QuartzJobServiceImpl implements QuartzJobService {
	private Logger logger = Logger.getLogger(getClass());
/*	@Autowired
	private SchedulerFactoryBean schedulerFactoryBean;*/
	@Resource
	private Scheduler scheduler;
	@Override
	public void addNewJob(IQuartzJobEntity jobEntity) throws SchedulerException {
		//Scheduler scheduler = schedulerFactoryBean.getScheduler();
		// 获取触发器key
		TriggerKey triggerKey = TriggerKey.triggerKey(
				jobEntity.getTriggerName(), jobEntity.getGroupName());
		// 获取任务触发器
		CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
		if (null == trigger) {
			// 初始化jobDetail
			JobDetail job = JobBuilder
					.newJob(QuartzJobBean.class)
					.withIdentity(jobEntity.getJobName(),
							jobEntity.getGroupName()).build();
			// 将jobname放到jobDataMap中
			job.getJobDataMap().put("targetObjectId", jobEntity.getJobName());
			// 将要调用的方法放入到 jobdatamap中
			job.getJobDataMap().put("targetObjectMethos", jobEntity.getCronMethod());
			// 表达式调度构建器
			CronScheduleBuilder scheduleBuilder = CronScheduleBuilder
					.cronSchedule(jobEntity.getCronExpression());
			trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey)
					.withSchedule(scheduleBuilder).build();
			// 启动任务
			scheduler.scheduleJob(job, trigger);
		} else {
			// 表达式调度构建器
			CronScheduleBuilder scheduleBuilder = CronScheduleBuilder
					.cronSchedule(jobEntity.getCronExpression());
			// 按新的cronExpression表达式重新构建trigger
			trigger = trigger.getTriggerBuilder().withIdentity(triggerKey)
					.withSchedule(scheduleBuilder).build();
			// 按新的trigger重新设置job执行
			scheduler.rescheduleJob(triggerKey, trigger);
			logger.info("任务已存在,更新任务调度重新开始！");
		}

	}

	@Override
	public void pauseJob(IQuartzJobEntity jobEntity) throws SchedulerException {
		//Scheduler scheduler = schedulerFactoryBean.getScheduler();
		JobKey jobKey = JobKey.jobKey(jobEntity.getJobName(),
				jobEntity.getGroupName());
		scheduler.pauseJob(jobKey);
		logger.info("暂停任务:"+jobEntity.toString());
	}

	@Override
	public void resumeJob(IQuartzJobEntity jobEntity) throws SchedulerException {
		//Scheduler scheduler = schedulerFactoryBean.getScheduler();
		JobKey jobKey = JobKey.jobKey(jobEntity.getJobName(),
				jobEntity.getGroupName());
		scheduler.resumeJob(jobKey);
		logger.info("恢复任务:"+jobEntity.toString());

	}

	@Override
	public void deleteJob(IQuartzJobEntity jobEntity) throws SchedulerException {
		//Scheduler scheduler = schedulerFactoryBean.getScheduler();
		JobKey jobKey = JobKey.jobKey(jobEntity.getJobName(),
				jobEntity.getGroupName());
		scheduler.deleteJob(jobKey);
		logger.info("删除任务:"+jobEntity.toString());
	}

	@Override
	public void triggerJob(IQuartzJobEntity jobEntity) throws SchedulerException {
		//Scheduler scheduler = schedulerFactoryBean.getScheduler();
		JobKey jobKey = JobKey.jobKey(jobEntity.getJobName(),
				jobEntity.getGroupName());
		scheduler.triggerJob(jobKey);
		logger.info("立刻运行任务:"+jobEntity.toString());

	}

	@Override
	public void rescheduleJob(IQuartzJobEntity jobEntity)
			throws SchedulerException {
		//Scheduler scheduler = schedulerFactoryBean.getScheduler();
		TriggerKey triggerKey = TriggerKey.triggerKey(jobEntity.getTriggerName(),
				jobEntity.getGroupName());
		// 获取trigger，即在spring配置文件中定义的 bean id="myTrigger"
		CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
		// 表达式调度构建器
		CronScheduleBuilder scheduleBuilder = CronScheduleBuilder
				.cronSchedule(jobEntity.getCronExpression());
		// 按新的cronExpression表达式重新构建trigger
		trigger = trigger.getTriggerBuilder().withIdentity(triggerKey)
				.withSchedule(scheduleBuilder).build();
		// 按新的trigger重新设置job执行
		scheduler.rescheduleJob(triggerKey, trigger);
		logger.info("更新任务时间:"+jobEntity.toString());

	}
}
